package com.atguigu.gmall.model.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.model.list.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 搜索的controller控制器
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月05日 15:41
 * @ClassName:SearchController
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/api/list")
public class SearchController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private SearchService  searchService;

    /**
     * 创建索引和映射
     * @return
     */
    @GetMapping("/create")
    public Result createIndexAndMapping(){
    //创建索引
       elasticsearchRestTemplate.createIndex(Goods.class);
       //船舰映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        //返回
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/lowerGoods/{skuId}")
    public  Result lowerGoods(Long skuId){
        searchService.lowerGoods(skuId);
        return   Result.ok();
    }

    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/upperGoods/{skuId}")
    public  Result upperGoods(@PathVariable(value = "skuId") Long skuId){
       searchService.upperGoods(skuId);
        return   Result.ok();
    }
    /**
     * 记录商品的热度值
     * @param skuId
     */
    @GetMapping(value = "/addHotScore/{skuId}")
    public Result addHotScore(@PathVariable(value = "skuId")Long skuId){
        searchService.addHotScore(skuId);
        return   Result.ok();
    }

    /**
     *
     * @param searchParam
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public SearchResponseVo search(SearchParam searchParam, @RequestParam Map<String,String> searchMap){
        SearchResponseVo search = searchService.search(searchParam, searchMap);
        return search;
    }
}
